angular.module('CheckConfirm')
    .controller('CheckConfirm.ScanCtrl', ['$scope', '$state', 'Http', 'imanDialog', 'Device', 'CheckConfirm.CheckService', 'LfRfid',
        function ($scope, $state, Http, imanDialog, Device, checkService, LfRfid) {
            var toast, date = new Date();
            $scope.count = checkService.getCount();

            $scope.showToast = function (styleMes, block) {
                if (toast) {
                    toast.hide();
                }
                toast = imanDialog.toast({
                    message: styleMes,
                    block: block,
                    position: 'top',
                    duration: false,
                    backdropClickToClose: false,
                    hardwareBackButtonClose: false
                });
            };

            $scope.hideToast = function () {
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            };

            $scope.$on('$ionicView.afterEnter', function () {
                LfRfid.open();
                // searchBarcode('0005604931');
                // searchBarcode('0005603076');
            });

            $scope.$on('$ionicView.beforeLeave', function () {
                LfRfid.close();
                checkService.setCount = {};
                $scope.hideToast();
            });

            $scope.$on('LFRFID.DataReceived', function (e, data) {
                searchBarcode(data);
            });

            $scope.checkFinish = function () {
                $state.go('CheckConfirm-result');
            };

            $scope.init = function () {
                if (checkService.getIsUpdateMsg()) {
                    checkService.queryKanBanMsg(Device.get().Department._id).then(function (obj) {
                        checkService.setScanQualifiedObj(obj);
                    }, function () {
                        imanDialog.toastBottom("获取看板信息失败");
                    });
                }
                var myCanvas = document.getElementById("myCanvas");
                var contextOne, contextTwo, contextThree, contextFour;
                contextOne = contextTwo = contextThree = contextFour = myCanvas.getContext("2d");
                resizeCanvas();
                angular.element(window).bind('resize', resizeCanvas);
                function resizeCanvas() {
                    var changeWidth = angular.element('.checkConfirm-scan .im-flow-container').innerWidth();
                    myCanvas.width = changeWidth;
                    myCanvas.height = 180;
                    contextOne.lineWidth = 2;
                    contextOne.beginPath();
                    contextOne.strokeStyle = '#ddd';
                    contextOne.moveTo((changeWidth / 2) - 40, 60);
                    contextOne.lineTo(70, 45);
                    contextOne.stroke();

                    contextTwo.lineWidth = 2;
                    contextTwo.beginPath();
                    contextTwo.strokeStyle = '#ddd';
                    contextTwo.moveTo((changeWidth / 2) - 40, 120);
                    contextTwo.lineTo(70, 140);
                    contextTwo.stroke();

                    contextThree.lineWidth = 2;
                    contextThree.beginPath();
                    contextThree.strokeStyle = '#ddd';
                    contextThree.moveTo((changeWidth / 2) + 40, 60);
                    contextThree.lineTo(changeWidth - 70, 45);
                    contextThree.stroke();

                    contextFour.lineWidth = 2;
                    contextFour.beginPath();
                    contextFour.strokeStyle = '#ddd';
                    contextFour.moveTo((changeWidth / 2) + 40, 120);
                    contextFour.lineTo(changeWidth - 70, 140);
                    contextFour.stroke();
                }
                checkService.BarcodeIsExistRcvRecord(checkService.getContainer());
            };

            $scope.init();
            
            function searchBarcode(barcode) {
                if (checkService.scanRepeat(barcode)) {
                    imanDialog.toastBottom('信息重复扫描');
                    return;
                }
                imanDialog.showLoading();
                checkService.queryBarcodeMaster(barcode).then(function (barcodeMasterInfo) {
                    if (barcodeMasterInfo && barcodeMasterInfo.BarcodeType === 0) {
                        if (angular.isUndefined(barcodeMasterInfo.QCStatus)) {
                            imanDialog.toastBottom('物料没有质检');
                            return;
                        }
                        if(checkService.arrangeData(barcodeMasterInfo)){
                            checkService.getRcvTime(barcodeMasterInfo).then(function(timeObj) {
                                $scope.RcvTime = timeObj.d + '天' + timeObj.h + '小时' + timeObj.mf + '分钟';
                            });
                            $scope.barcodeInfo = barcodeMasterInfo;
                            $scope.count = checkService.getCount();
                        }
                    } else {
                        imanDialog.toastBottom('扫描对象不是物料');
                    }
                }, function () {
                    imanDialog.toastBottom('芯片未识别');
                }).finally(function () {
                    imanDialog.hideLoading();
                });
            }

            function getRcvTime(result) {
                var milliseconds = date.setMilliseconds(0) - (new Date(result.RcvDateTime ? result.RcvDateTime : new Date()).setMilliseconds(0));
                var ms = milliseconds / 1000 / 60;
                var m = ms % 60;
                var mf = Math.round(m);
                var h = ((ms - m) / 60) % 24;
                var d = (((ms - m) / 60) - ((ms - m) / 60) % 24) / 24;
                return {
                    d: d,
                    h: h,
                    mf: mf
                };
            }
        }
    ]);
